package com.chen.controller;


import com.chen.model.Goods;
import com.chen.model.Imports;
import com.chen.service.GoodsService;
import com.chen.service.ImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/imports")
public class ImportController {

    @Autowired
    private ImportService importService;

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/import-add")
    public String importAdd(Integer goods_id,Model model){
        Goods goods = goodsService.findGoodsById(goods_id);
        System.out.println(goods);
        System.out.println(goods_id);
        model.addAttribute("goods",goods);
        return "import-add";

}
    @RequestMapping("/addImport")
    public String addImport(Imports imports, HttpServletRequest request){
        Goods goods = new Goods();
//        Integer goods_id = (Integer) request.getAttribute("goods_id");
//        int number = (int) request.getAttribute("import_number");
        int goods_id = Integer.parseInt(request.getParameter("goods_id")) ;
        goods.setGoods_id(goods_id);
        imports.setGoods(goods);
        imports.setImport_state(0);
        imports.setImport_time(new Date());
        System.out.println(goods.getGoods_id());
        System.out.println(goods_id);
        importService.addImport(imports);
        return "success";
    }

    @RequestMapping("/findAllImports")
    public String findAllImports(Model model){
        List<Imports> importsList;
        importsList = importService.findAllImports();
        System.out.println(importsList.toString());
        model.addAttribute("importsList",importsList);
        return "import-list";
    }


}
